//
//  BaseCacheManager+KeyChain.m
//  LotteryClient
//
//  Created by Suny on 14-8-18.
//  Copyright (c) 2014年 CTTC. All rights reserved.
//

#import "BaseCacheManager+KeyChain.h"
#import <objc/runtime.h>
#import "SFHFKeychainUtils.h"


static char *serviceNameKey = "serviceNameKey" ;

@implementation BaseCacheManager (KeyChain)

- (NSString *)serviceName
{
	NSString *serviceNameString = nil;
	serviceNameString = objc_getAssociatedObject(self, serviceNameKey);
	if (!serviceNameString)
	{
		serviceNameString = kAppSignIdentifier;
	}
	
	return serviceNameString;
}


- (void)setServiceName:(NSString *)serviceName
{
	if (serviceName)
	{
		objc_setAssociatedObject(self, serviceNameKey, serviceName, OBJC_ASSOCIATION_COPY);
	}
	else
	{
		objc_setAssociatedObject(self, serviceNameKey, nil, OBJC_ASSOCIATION_ASSIGN);
	}
}

- (BOOL)saveInKeyChainValueString:(NSString *)valueString
						   forKey:(NSString *)keyString
				   updateExisting:(BOOL)update
							error:(NSError **)error;
{
	return [SFHFKeychainUtils storeUsername:keyString
								andPassword:valueString
							 forServiceName:self.serviceName
							 updateExisting:update
									  error:error];
}


- (NSString *)valueInKeyChainForKey:(NSString *)key
{
	return [SFHFKeychainUtils getPasswordForUsername:key
									  andServiceName:self.serviceName
											   error:nil];
}

- (BOOL)deleteValueInKeyChainForKey:(NSString *)key
{
	return [SFHFKeychainUtils deleteItemForUsername:key andServiceName:self.serviceName error:nil];
}

- (BOOL)deleteValueInKeyChainForKey:(NSString *)key error:(NSError **)error
{
	return [SFHFKeychainUtils deleteItemForUsername:key andServiceName:self.serviceName error:error];
}
@end
